var MyTable={
    searchForm:"searchForm",
    searchUrl:"/wxmessage/list",
	tableId:"lgTable",
	table:null,
	prefix:"/wxmessage"
};

$(function() {
    var defaultColunms = MyTable.initColumn();
    var table = new BSTable(MyTable.tableId, MyTable.searchUrl,defaultColunms);
	table.setQueryParams(getFormJson(MyTable.searchForm));
	//table.setSortName("asc");
	//table.setSortOrder("gmt_create");
	MyTable.table = table.init(0);
});
function reLoad() {
	MyTable.table.refresh(getFormJson(MyTable.searchForm));
}
function resetSearch(){
	cleanFormVal(MyTable.searchForm);
	MyTable.table.refresh(null);
}



MyTable.initColumn = function () {
    var columns =  [
		{checkbox : true},
		{field : 'id', title : ''},
		{field : 'mediaKey', title : '素材key'},
		{field : 'msgType', title : '消息类型'},
		{field : 'content', title : '消息内容'},
		/*{field : 'openids', title : '发送用户'},
		{field : 'groupId', title : '分组'},*/
		{field : 'gmtCreate', title : '创建时间'},
		{
			title : '操作',
			field : 'id',
			align : 'center',
			formatter : function(value, row, index) {
				var e = '<a  class="btn btn-primary btn-sm ' + editBtn + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
				+ row.id 
				+ '\')"><i class="fa fa-edit "></i></a> ';
			var d = '<a class="btn btn-warning btn-sm ' + delBtn + '" href="#" title="删除"  mce_href="#" onclick="delRow(\''
				+ row.id 
				+ '\')"><i class="fa fa-remove"></i></a> ';
			return e + d;
			}
		} ];
    return columns;
};

function add() {
	layer.open({
		type : 2,
		title : '创建图文消息',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '670px' ],
		content : MyTable.prefix + '/add/'
	});
}
function edit(userId) {
	layer.open({
		type : 2,
		title : '修改xx',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : MyTable.prefix+'/edit/'+userId
	});
}
function delRow(id){
    var config={
			url : MyTable.prefix+"/deleteRow",
			data:{"id":id},
			sureDo:true,
			msg:"确定删除记录？"
	}
	EasyAjax.post_json(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			reLoad();
		} 
	});
}
function delRows(){
	var rows=checkSelect();
	if(!rows)
	return;
    var config={
			url : MyTable.prefix+"/deleteRows",
			data: {"ids" : rows},
			sureDo:true,
			msg:"确定删除记录？"
	}
	EasyAjax.post(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			reLoad();
		} 
	});
}
function checkSelect(){
	var rows = $('#'+MyTable.tableId).bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return null;
	}else{
		var ids = new Array();
		$.each(rows, function(i, row) {
			ids[i] = row.userId;
		});
		return ids;
	}
}
